package net.sycu.wj.ems.servlet.user;

import java.io.IOException;



import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sycu.wj.ems.business.LoginLogService;
import net.sycu.wj.ems.entity.LoginBean;
import net.sycu.wj.ems.entity.LoginLogBean;
import net.sycu.wj.ems.entity.LoginLogSearcher;
import net.sycu.wj.ems.entity.LoginSearcher;
import net.sycu.wj.ems.servlet.BaseServlet;

public class ShowLoginLogsServlet extends BaseServlet {
	
	LoginLogService service = new LoginLogService();
	//查询分页排序都写到doGet()
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sortField = "LoginLogId";
		if(request.getParameter("hidSortField") != null){
			sortField = this.getParameter("hidSortField");
		}
		
		String sortString = "LoginLogId ASC";
		if(request.getParameter("hidSortString") != null){
			sortString = this.getParameter("hidSortString");
		}
		
		if(this.getParameter("hidOperateType").compareToIgnoreCase("sorting") == 0){
			if(sortString.indexOf(sortField) >= 0){
				if(sortString.indexOf("ASC") >= 0)
					sortString = sortString.replace("ASC", "DESC");
				else
					sortString = sortString.replace("DESC", "ASC");
			}
			else
				sortString = sortField + " ASC";
		}
			
		
		LoginLogSearcher searcher = new LoginLogSearcher();
		searcher.setLoginIp(this.getParameter("txtSearchLoginIp"));
		if(this.getParameter("txtSearchLoginDateTimeBegin").length() > 0)
			searcher.setLoginDateTimeBegin(this.getTimestampParameter("txtSearchLoginDateTimeBegin"));
		
		if(this.getParameter("txtSearchLoginDateTimeEnd").length() > 0)
			searcher.setLoginDateTimeEnd(this.getTimestampParameter("txtSearchLoginDateTimeEnd"));

		searcher.setLogin(new LoginSearcher());
		searcher.getLogin().setLoginName(this.getParameter("txtSearchLoginName"));
		
		ArrayList<LoginLogBean> loginLogs = service.search(searcher, sortString,this.getLogin().getLoginId());
		
		int rowCount = loginLogs.size();
		int pageCount = 1;

		int pageSize = 10;
		if(request.getParameter("hidPageSize") != null){
			pageSize = this.getIntegerParameter("hidPageSize");
		}
		if(pageSize <= 1)
			pageSize = 2;
		
		
		
		pageCount = rowCount / pageSize + (rowCount % pageSize == 0 ? 0 : 1);//能整除就�?，不能整除就�?
		//如果没有数据pageCount=0
		if(pageCount <= 0)
			pageCount = 1;

		int currentPageIndex = 1;
		if(request.getParameter("hidCurrentPageIndex") != null){
			currentPageIndex = this.getIntegerParameter("hidCurrentPageIndex");
		}
		if(currentPageIndex <= 0)
			currentPageIndex = 1;
		if(currentPageIndex > pageCount)
			currentPageIndex = pageCount;
		
		
		request.setAttribute("currentPageIndex", currentPageIndex);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("rowCount", rowCount);
		request.setAttribute("searcher", searcher);
		request.setAttribute("sortString", sortString);
		request.setAttribute("sortField", sortField);
		
		
		request.setAttribute("loginLogs", loginLogs);//以前带的是一句话，现在带的是�?��集合
		request.getRequestDispatcher("./showLoginLogs.jsp").include(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	
}
